package com.hdu.seckilldemo.controller;

import com.hdu.seckilldemo.pojo.User;
import com.hdu.seckilldemo.service.IGoodsService;
import com.hdu.seckilldemo.service.impl.UserServiceImpl;
import com.hdu.seckilldemo.vo.DetailVo;
import com.hdu.seckilldemo.vo.GoodsVo;
import com.hdu.seckilldemo.vo.RespBean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.Thymeleaf;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.HTML;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;//手动渲染

    @RequestMapping(value = "/toList", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model,User user,HttpServletRequest request, HttpServletResponse response){
/*     这里使用自定义参数校验直接校验用户登陆情况
        //如果没有登陆信息，跳转到登陆页面
        if (StringUtils.isEmpty(ticket)) {
            return "login";
        }
        //如果有，我们从redis中获取用户信息
        User user = userService.getUserByCookie(ticket,request,response);
        */
        // 优化： 添加redis作为页面缓存
        // 1. 首先查找redis中是否已经缓存了页面
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsList");
        if(!StringUtils.isEmpty(html)){
            //如果不为空，直接返回缓存的页面
            return html;
        }
        //如果找到了用户信息 就把user传到前端中去
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        //如果为空 就需要手动渲染，并且存入redis后返回
        WebContext context = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap()); //传入数据 只需要model转成map就可以
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList", context);
        //渲染成功,加入redis，并且设置时效
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,60, TimeUnit.SECONDS);
        }
        return html;
    }
    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(Model model, User user, @PathVariable Long goodsId){
        DetailVo detailVo = new DetailVo();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        //计算秒杀的状态
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date now = new Date();
        int secKillStatus = 0;
        int reaminSecond = 0; //秒杀倒计时
        if(now.before(startDate)){
            //secKillStatus = 0; //秒杀尚未开始
            reaminSecond = ((int) (startDate.getTime() - now.getTime())/ 1000);
        }else if(now.after(endDate)){
            secKillStatus = 2; //秒杀已经结束
            reaminSecond = -1;
        }else{
            secKillStatus = 1;//秒杀中
            reaminSecond = 0;
        }
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVo);
        detailVo.setSecKillStatus(secKillStatus);
        detailVo.setRemainSeconds(reaminSecond);
        //处理完 返回detailVo对象
        return RespBean.success(detailVo);
    }

}
